﻿using HrEFCore;
using HrEFCore.Models;
using HrIService;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HrService
{
    public class CFTKService : ICFTKService
    {
        private readonly MyDbContext myDbContext;
		
		public CFTKService(MyDbContext myDbContext)
        {
            this.myDbContext = myDbContext;
        }
		public Task<List<CFTK>> SelectCFTKAsync()
		{
			return myDbContext.CFTK.ToListAsync();
		}

		public async Task<int> AddCFTKAsync(CFTK cftk)
		{
			myDbContext.CFTK.AddAsync(cftk);
			return await myDbContext.SaveChangesAsync();
		}

		public async Task<int> DeleteCFTKAsync(short FtkId)
		{
			var zhi = await myDbContext.CFTK.FindAsync(FtkId);
			if(zhi == null)
			{
				return 0;
			}
			myDbContext.CFTK.Remove(zhi);
			return await myDbContext.SaveChangesAsync();
		}

		public Task<int> UpdateCFTKAsync(CFTK cftk)
		{
			myDbContext.CFTK.Update(cftk);
			return  myDbContext.SaveChangesAsync();
		}

		public Task<List<CFFK>> CFFKSelectAsync()
		{
			return myDbContext.CFFK.ToListAsync();
		}

		public Task<List<CFSK>> CFSKSelectAsync()
		{
			return myDbContext.CFSK.ToListAsync();
		}

		public async Task<CFTK> CFTKByIdAsync(short FtkId)
		{
			return await myDbContext.CFTK.FindAsync(FtkId);
		}
		
		public async Task<CFFK> CFFKByIdAsync(string firstkindId)
		{
			return await myDbContext.CFFK.FirstOrDefaultAsync(e=>e.FirstKindId==firstkindId);
		}

		public async Task<CFSK> CFSKByIdAsync(string secondkindId)
		{
			return await myDbContext.CFSK.FirstOrDefaultAsync(e=>e.SecondKindId==secondkindId);
		}

		public async Task<List<CFSK>> CFSKLDAsync(string id)//一二级联动
		{
			return await myDbContext.CFSK.Where(e => e.FirstKindId == id).ToListAsync();
		}

		public async Task<List<CFTK>> CFTKLDAsync(string id)//二三级联动
		{
			return await myDbContext.CFTK.Where(e=>e.SecondKindId == id).ToListAsync();
		}
	}
}
